<?php
/**
 *  生成pdf
 */
require_once './vendor/autoload.php';

class openpdfClassAction extends openapiAction
{
    public function __construct()
    {
        parent::__construct();
        //开启调试模式
        define('APP_DEBUG', true);
        //包含自动加载类
        //smarty实例化
        $this->smarty = new \Smarty;
        //$smarty->left_delimiter = "{#";
        //$smarty->right_delimiter = "#}";
        $this->smarty->setTemplateDir(ROOT_PATH . '/views/'); //设置模板目录
        $this->smarty->setCompileDir(ROOT_PATH . '/upload/cache/templates_c/');
//        $smarty->setConfigDir(ROOT_PATH . '/views/smarty_configs/');
        $this->smarty->setCacheDir(ROOT_PATH . '/upload/cache/smarty_cache/');
        //$smarty->force_compile = true;
        if (APP_DEBUG) {
            //$smarty->debugging      = true;
            $this->smarty->caching        = false;
            $this->smarty->cache_lifetime = 0;
        } else {
            //$smarty->debugging      = false;
            $this->smarty->caching        = true;
            $this->smarty->cache_lifetime = 120;
        }
    }

    /**
     * 执行采购审批表html
     * url:/api.php?m=openpdf&a=cgwjspb
     * @param $id  cg_dljg_flow 主键
     */
    public function cgwjspbAction(){
        $id = $this->request("id");
        if(empty($id)) return '数据不能为空';
        $data = m('cg_dljg_flow')->getone('id='.$id, 'proxycompany,beian_num,projectname,receivetime,lastsubtime,bugtype,proxycompanylink,proxycompanylinktel,zbzxlink,zbzxlinktel,dljgname,dljglink,dljglinktel,budget');
        if(!empty($data['receivetime'])) $data['receivetime'] = date('Y-m-d', strtotime($data['receivetime']));
        $dataRow = m('flow_log')->getall('`mid`='.$id." and `table` = 'cg_dljg_flow' and `status` = 1",'`courseid`, `checkname`, `checkid`, `optdt`, `explain`', 'optdt asc');
        $imgs = array();
        foreach ($dataRow as $key => $value) {
            if(($value['courseid'] == 155) || ($value['courseid'] == 211) || ($value['courseid'] == 212)) {
                $userInfo = m('admin')->getone('id='.$value['checkid'], 'ranking');
                $ranking = $userInfo['ranking'];
            }
            $signPath = m('option')->getval('qmimgstr_'.$value['checkid']);
            $sealPath = m('option')->getval('signature_'.$value['checkid']);
            $personalPath = m('option')->getval('personal_'.$value['checkid']);
            $time = date("Y年m月d日", strtotime($value['optdt']));
            if($data['budget'] >= 1000000) {
                switch ($value['courseid']) {
                    case 183://项目负责人
                        $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                        if($path === false) return "请项目负责人检测签名是否存在";
                        $imgs['xmfzrName'] = $path;
                        $data['xmfzrTime'] = $time;
                        $data['xmfzrExplain'] = $value['explain'];
                        break;
                    case 181://采购科科长
                        $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                        if($path === false) return "请采购科科长检测签名是否存在";
                        $imgs['zbzxcgkName'] = $path;
                        $data['zbzxcgkTime'] = $time;
                        $data['zbzxcgkExplain'] = $value['explain'];
                        break;
                    case 153://装备中心分管主任
                        if(empty($sealPath) || empty($signPath) || empty(file_exists($sealPath)) || empty(file_exists($signPath))) return '请分管主任检测签名与公章是否存在';

                        $imgs['fgldName'] = $signPath;
                        $imgs['fgldSeal'] = $sealPath;
                        $data['fgldTime'] = $time;
                        $data['fgldExplain'] = $value['explain'];
                        break;
                    case 154://装备中心主任
                        if(empty($sealPath) || empty($signPath) || empty(file_exists($sealPath)) || empty(file_exists($signPath))) return '请装备中心主任检测签名与公章是否存在';
                        $imgs['zbzxfzrName'] = $signPath;
                        $imgs['zbzxfzrSeal'] = $sealPath;
                        $data['zbzxfzrTime'] = $time;
                        $data['zbzxfzrExplain'] = $value['explain'];
                        break;
                    case 155://采购单位
                        if($ranking == '项目负责人') {
                            if(empty($sealPath) || empty($signPath) || empty(file_exists($sealPath)) || empty(file_exists($signPath))) return '请采购单位负责人检测签名与公章是否存在';
                            $imgs['wtdwName'] = $signPath;
                            $imgs['wtdwSeal'] = $sealPath;
                            $data['wtdwTime'] = $time;
                            $data['wtdwExplain'] = $value['explain'];
                        }
                        break;
                    default:
                        break;
                }
//            }elseif ($data['budget'] >= 500000 && $data['budget'] <= 800000) {
            }elseif ($data['budget'] < 1000000) {
                switch ($value['courseid']) {
                    case 213://项目负责人
                        $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                        if($path === false) return "请项目负责人检测签名是否存在";
                        $imgs['xmfzrName'] = $path;
                        $data['xmfzrTime'] = $time;
                        $data['xmfzrExplain'] = $value['explain'];
                        break;
                    case 202://采购科科长
                        $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                        if($path === false) return "请采购科科长检测签名是否存在";
                        $imgs['zbzxcgkName'] = $path;
                        $data['zbzxcgkTime'] = $time;
                        $data['zbzxcgkExplain'] = $value['explain'];
                        break;
                    case 204://装备中心分管主任
                        if(empty(file_exists($sealPath)) || empty(file_exists($signPath))) return '请分管主任检测签名与公章是否存在';
                        $imgs['fgldName'] = $signPath;
                        $imgs['fgldSeal'] = $sealPath;
                        $data['fgldTime'] = $time;
                        $data['fgldExplain'] = $value['explain'];
                        break;
                    case 211://采购单位
                        if($ranking != '项目负责人') break;
                        if(empty(file_exists($sealPath)) || empty(file_exists($signPath))) return '请采购单位负责人检测签名与公章是否存在';
                        $imgs['wtdwName'] = $signPath;
                        $imgs['wtdwSeal'] = $sealPath;
                        $data['wtdwTime'] = $time;
                        $data['wtdwExplain'] = $value['explain'];
                        break;
                    default:
                        break;
                }
                $imgs['zbzxfzrName'] = '';
                $imgs['zbzxfzrSeal'] = '';
                $data['zbzxfzrTime'] = '';
                $data['zbzxfzrExplain'] = '';
            }else {
                switch ($value['courseid']) {
                    case 214://项目负责人
                        $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                        if($path === false) return "请项目负责人检测签名是否存在";
                        $imgs['xmfzrName'] = $path;
                        $data['xmfzrTime'] = $time;
                        $data['xmfzrExplain'] = $value['explain'];
                        break;
                    case 203://采购科科长
                        $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                        if($path === false) return "请采购科科长检测签名是否存在";
                        $imgs['zbzxcgkName'] = $path;
                        $data['zbzxcgkTime'] = $time;
                        $data['zbzxcgkExplain'] = $value['explain'];
                        break;
                    case 212://采购单位
                        if($ranking != '项目负责人') break;
                        if(empty(file_exists($sealPath)) || empty(file_exists($signPath))) return '请采购单位负责人检测签名与公章是否存在';
                        $imgs['wtdwName'] = $signPath;
                        $imgs['wtdwSeal'] = $sealPath;
                        $data['wtdwTime'] = $time;
                        $data['wtdwExplain'] = $value['explain'];
                        break;
                    default:
                        break;
                }
                $imgs['zbzxfzrkName'] = '';
                $data['zbzxfzrTime'] = '';
                $data['zbzxfzrExplain'] = '';
                $data['fgldName'] = '';
                $data['fgldTime'] = '';
                $data['fgldExplain'] = '';
            }
        }

        if(empty($imgs['xmfzrName'])){
            $imgs['xmfzrName'] = "";
            $data['xmfzrTime'] = "";
            $data['xmfzrExplain'] = "";
        }
        if(empty($imgs['zbzxcgkName'])){
            $imgs['zbzxcgkName'] = "";
            $data['zbzxcgkTime'] = "";
            $data['zbzxcgkExplain'] = "";
        }
        if(empty($imgs['fgldName']) || empty($imgs['fgldSeal'])){
            $imgs['fgldName'] = '';
            $imgs['fgldSeal'] = '';
            $data['fgldTime'] = '';
            $data['fgldExplain'] = '';
        }
        if(empty($imgs['zbzxfzrName']) || empty($imgs['zbzxfzrSeal'])){
            $imgs['zbzxfzrName'] = '';
            $imgs['zbzxfzrSeal'] = '';
            $data['zbzxfzrTime'] = '';
            $data['zbzxfzrExplain'] = '';
        }
        if(empty($imgs['wtdwName'])){
            $imgs['wtdwName'] = "";
            $data['wtdwTime'] = "";
            $data['wtdwExplain'] = "";
        }
        if(empty($data)) return '数据不能为空';
        $this->smarty->assign('imgs', $imgs);
        $this->smarty->assign('data', $data);
        $this->smarty->display('cgwjspb.html');
    }

    /**
     * 采购项目委托单html
     * url:/api.php?m=openpdf&a=cgsq
     * @param $id
     */
    public function cgsqAction()
    {
        $id = $this->request("id");
        $new = m('zbcg_new')->getone('id='.$id, 'proxycompany, company_address, companyadminer, processer, linktel');
        $data = array();
        isset($new['proxycompany']) && $data['companyName'] = $new['proxycompany'];
        isset($new['company_address']) && $data['address'] = $new['company_address'];
        isset($new['processer']) && $data['agent'] = $new['processer'];
        isset($new['linktel']) && $data['mobile'] = $new['linktel'];
        $dataRow = m('zbcg_new_info')->getrows('mid = '. $id, 'dept, name, type, producing_area, budget, budget_item_name, funds_type, detail, other', 'id asc');
        if(empty($dataRow)) return '数据不能为空';
        $total = 0;
        $number = 0;
        foreach ($dataRow as $key => $value) {
            $number += 1;
            $total += $value['budget'];
            $dataRow[$key]['rowNumber'] = $number;
            $dataRow[$key]['dept'] = $new['proxycompany'];
            if(!empty($value['dept'])) {
                $dataRow[$key]['dept'] = $new['proxycompany'].'-'.$value['dept'];
            }
        }
        $data['budget'] = $total;

        $logList = m('flow_log')->getall('`mid`='.$id." and `table` = 'zbcg_new' and `status` = 1",'checkid, courseid', 'optdt asc');
        foreach ($logList as $k => $v) {
            if ($v['courseid'] == 112) {
                $signPath = m('option')->getval('qmimgstr_'.$v['checkid']);
                $personalPath = m('option')->getval('personal_'.$v['checkid']);
                $sealPath = m('option')->getval('signature_'.$v['checkid']);
                $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                if($path === false) return "请单位负责人检测签名是否存在";
                if (empty(file_exists($sealPath))) return "请单位负责人检测公章是否存在";

                $data['sign'] = $signPath;
                $data['seal'] = $sealPath;
            }
        }
        $this->smarty->assign('dataRow', $dataRow);
        $this->smarty->assign('data', $data);
        $this->smarty->display('cgsq.html');
    }

    /**
     * 抽取专家申请表页面
     * url:/api.php?m=openpdf&a=cqzjsqb
     * @param $id  cg_dljg_flow 主键
     */
    public function cqzjsqAction(){
        $id = $this->request("id");
        if(empty($id)) return '数据不能为空';
        $data = m('zbcg_expertappy')->getone('id='.$id);
        $dataRow = m('flow_log')->getall('`mid`='.$id." and `table` = 'zbcg_expertappy' and `status` = 1",'courseid, step as rowNumber,name as action, checkname, optdt as time,statusname as checkStatus, checkid', 'optdt asc');
        if(empty($dataRow)) return json_encode(array('code' => 300, 'msg'=>'数据为空'));

        $imgs = array();
        foreach ($dataRow as $key => $value) {
            $signPath = m('option')->getval('qmimgstr_'.$value['checkid']);
            $personalPath = m('option')->getval('personal_'.$value['checkid']);
            $time = $value['time']?date("Y年m月d日", strtotime($value['time'])):'';
            switch ($value['courseid']) {
                case 185://项目负责人
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if($path === false) return "请项目负责人检测签名是否存在";
                    $imgs['xmfzrname'] = $path;
                    $data['xmfzrdate'] = $time;
                    break;
                case 160:// 采购科长
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if($path === false) return "请采购科科长检测签名是否存在";
                    $imgs['cgkzname'] = $path;
                    $data['cgkzdate'] = $time;
                    break;
                case 184:// 装备中心主任
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if($path === false) return "请装备中心主任检测签名是否存在";
                    $imgs['cgzxzrname'] = $path;
                    $data['cgzxzrdate'] = $time;
                    break;
                default:
                    break;
            }
            unset($value['checkid']);
            $dataRow[$key]['rowNumber'] = $key + 1;
        }
        if(empty($imgs['xmfzrname'])){
            $imgs['xmfzrname'] = "";
            $data['xmfzrdate'] = "";
        }
        if(empty($imgs['cgkzname'])){
            $imgs['cgkzname'] = "";
            $data['cgkzdate'] = "";
        }
        if(empty($imgs['cgzxzrname'])){
            $imgs['cgzxzrname'] = "";
            $data['cgzxzrdate'] = "";
        }
        if(empty($data)) return '数据不能为空';
        $this->smarty->assign('dataRow', $dataRow);
        $this->smarty->assign('imgs', $imgs);
        $this->smarty->assign('data', $data);
        $this->smarty->display('cqzjsq.html');
    }

    /**
     * 采购结果审批表页面
     * url:/api.php?m=openpdf&a=cgjg
     * @param $id  cg_jieguo_list 主键
     * return office文件
     */
    public function cgjgAction(){
        $id = $this->request("id");
        if(empty($id)) return '数据不能为空';
        $data = m('cg_jieguo_list')->getone('id='.$id, 'name,beian_num,budget,zbmoney,caigoutype,zbtime');
        $dataRow = m('flow_log')->getall('`mid`='.$id." and `table` = 'cg_jieguo_list' and `status` = 1",'`courseid`, `checkname`, `checkid`, `optdt`, `explain`', 'optdt asc');
        if(empty($dataRow)) return json_encode(array('code' => 300, 'msg'=>'数据为空'));
        $imgs = array();
        foreach ($dataRow as $key => $value) {
            if($value['courseid'] == 232) {
                $userInfo = m('admin')->getone('id='.$value['checkid'], 'ranking');
                $ranking = $userInfo['ranking'];
            }
            $signPath = m('option')->getval('qmimgstr_'.$value['checkid']);
            $sealPath = m('option')->getval('signature_'.$value['checkid']);
            $personalPath = m('option')->getval('personal_'.$value['checkid']);
            $time = date("Y年m月d日", strtotime($value['optdt']));
            switch ($value['courseid']) {
                case 227://装备中心项目负责人审核
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if($path === false) return "请装备中心项目负责人检测签名是否存在";
                    $imgs['xmfzrName'] = $path;
                    $data['xmfzrTime'] = $time;
                    break;
                case 228://采购科长审核
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if($path === false) return "请采购科长检测签名是否存在";
                    $imgs['zbzxcgkName'] = $path;
                    $data['zbzxcgkTime'] = $time;
                    break;
                case 229://装备中心分管副主任审核
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if($path === false) return "请采购科长检测签名是否存在";
                    $imgs['fgldName'] = $path;
                    $data['fgldTime'] = $time;
                    break;
                case 230://装备中心主任审核
                    if(empty(file_exists($sealPath)) || empty(file_exists($signPath))) return '请装备中心主任检测签名与公章是否存在';
                    $imgs['zbzxfzrName'] = $signPath;
                    $imgs['zbzxfzrSeal'] = $sealPath;
                    $data['zbzxfzrTime'] = $time;
                    break;
                case 232://采购单位审核
                    if($ranking == '项目负责人') {
                        if (empty(file_exists($sealPath)) || empty(file_exists($signPath))) return '请采购单位项目负责人检测签名与公章是否存在';
                        $imgs['wtdwName'] = $signPath;
                        $imgs['wtdwSeal'] = $sealPath;
                        $data['wtdwTime'] = $time;
                    }
                    break;
                case 234://代理机构发布公示
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if(empty($path) || empty(file_exists($sealPath))) return "请代理机构检测签名与公章是否存在";
                    $imgs['dljgyijianName'] = $path;
                    $imgs['dljgyijianSeal'] = $sealPath;
                    $data['dljgyijianTime'] = $time;
                    $data['dljgyijianExplain'] = $value['explain'];
                    break;
                default:
                    break;
            }
        }

        if(!isset($imgs['xmfzrName'])){
            $imgs['xmfzrName'] = "";
            $data['xmfzrTime'] = "";
            $data['xmfzrExplain'] = "";
        }
        if(!isset($imgs['zbzxcgkName'])){
            $imgs['zbzxcgkName'] = "";
            $data['zbzxcgkTime'] = "";
            $data['zbzxcgkExplain'] = "";
        }
        if(!isset($imgs['fgldName'])){
            $imgs['fgldName'] = "";
            $data['fgldTime'] = "";
            $data['fgldExplain'] = "";
        }
        if(!isset($imgs['zbzxfzrName'])){
            $imgs['zbzxfzrName'] = "";
            $data['zbzxfzrTime'] = "";
            $data['zbzxfzrExplain'] = "";
        }
        if(!isset($imgs['wtdwName'])){
            $imgs['wtdwName'] = "";
            $data['wtdwTime'] = "";
            $data['wtdwExplain'] = "";
        }
        if(!isset($imgs['dljgyijianName'])){
            $imgs['dljgyijianName'] = '';
            $data['dljgyijianTime'] = '';
            $data['dljgyijianExplain'] = '';
            $imgs['dljgyijianSeal'] = '';
        }

        if(empty($data)) return '数据不能为空';
        $this->smarty->assign('imgs', $imgs);
        $this->smarty->assign('data', $data);
        $this->smarty->display('cgjg.html');
    }

    /**
     * 收文单页面
     * url:/api.php?m=openpdf&a=swd
     * @param $id  cg_dljg_flow 主键
     */
    public function swdAction(){
        $id = $this->request("id");
        if(empty($id)) return '数据不能为空';
        $loginfo = m('flow_log')->getone('`modeid`=94 and `courseid`=112 and `mid`='.$id, 'optdt');
        $newinfo  = m('zbcg_new')->getone('`id`='.$id, 'proxycompany,symbol,projectname,note');
        $bilinfo = m('flow_bill')->getone('`modeid`=94 and `mid`='.$id, 'sericnum');
        $data = array(
            'proxycompany' => $newinfo['proxycompany'],
            'symbol' => $newinfo['symbol'],
            'projectname' => $newinfo['projectname'],
            'sericnum' => $bilinfo['sericnum'],
            'note' => $newinfo['note']
        );
        $data['acceptdate'] = '';
        if(!empty($loginfo)) $data['acceptdate'] = date('Y-m-d', strtotime($loginfo['optdt']));
        $dataRow = m('flow_log')->getall('`modeid`=94 and `mid`='.$id.' and `status` = 1', 'courseid, step as rowNumber,name as action, checkname as name, optdt as time, `explain` as checkStatus, checkid', 'optdt asc');

        $imgs = array();
        foreach ($dataRow as $key => $value) {
            $signPath = m('option')->getval('qmimgstr_'.$value['checkid']);
            $personalPath = m('option')->getval('personal_'.$value['checkid']);
            $time = $value['time']?date("Y年m月d日", strtotime($value['time'])):'';
            switch ($value['courseid']) {
                case 114://装备采购科科长
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if($path === false) return "请采购科科长检测签名是否存在";
                    $imgs['xmfzrName'] = $path;
                    $data['xmfzrTime'] = $time;
                    $data['xmfzrExplain'] = $value['checkStatus']?$value['checkStatus']:'';
                    break;
                case 170://装备中心分管主任
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if($path === false) return "请装备中心分管主任检测签名是否存在";
                    $imgs['zbzxcgkName'] = $path;
                    $data['zbzxcgkTime'] = $time;
                    $data['zbzxcgkExplain'] = $value['checkStatus']?$value['checkStatus']:'';
                    break;
                case 171://装备中心主任
                    $path = file_exists($personalPath) ? $personalPath : (file_exists($signPath) ? $signPath : false);
                    if($path === false) return "请装备中心负责人检测签名是否存在";
                    $imgs['fgldName'] = $path;
                    $data['fgldTime'] = $time;
                    $data['fgldExplain'] = $value['checkStatus']?$value['checkStatus']:'';
                    break;
                default:
                    break;
            }
            unset($value['checkid']);
            $dataRow[$key]['rowNumber'] = $key + 1;
        }
        if(empty($imgs['fgldName'])){
            $imgs['fgldName'] = "";
            $data['fgldTime'] = "";
            $data['fgldExplain'] = "";
        }
        if(empty($imgs['zbzxcgkName'])){
            $imgs['zbzxcgkName'] = "";
            $data['zbzxcgkTime'] = "";
            $data['zbzxcgkExplain'] = "";
        }
        if(empty($imgs['xmfzrName'])){
            $imgs['xmfzrName'] = "";
            $data['xmfzrTime'] = "";
            $data['xmfzrExplain'] = "";
        }
        if(empty($data)) return '数据不能为空';
        $this->smarty->assign('dataRow', $dataRow);
        $this->smarty->assign('imgs', $imgs);
        $this->smarty->assign('data', $data);
        $this->smarty->display('swd.html');
    }
}